package leetcode;

public class RemoveDuplicates {

	public static void main(String[] args) {
		int[] nums = { 1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5 };
		RemoveDuplicates removeDuplicates = new RemoveDuplicates();
		removeDuplicates.removeDuplicatesWithTwice(nums);
	}

	public int removeDuplicates(int[] nums) {
		if (nums == null || nums.length <= 0) {
			return 0;
		}
		int count = 0;
		for (int i = 0; i < nums.length; i++) {
			while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
				i++;
			}
			nums[count++] = nums[i];
		}
		return count;
	}

	public int removeDuplicatesWithTwice(int[] nums) {
		if (nums == null || nums.length <= 0) {
			return 0;
		}
		int count = 0;
		boolean flag = false; //用于标识该数字出现了一次还是一次以上，一次为false
		for (int i = 0; i < nums.length; i++) {
			nums[count++] = nums[i];
			flag = false;
			while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
				i++;
				flag = true;
			}
			System.out.println("count:" + count);
			//如果只出现了一次，前面已经加了一次，这里无须再次加入
			if(flag){
				nums[count++] = nums[i];
			}
		}
		for (int i = 0; i < count; i++) {
			System.out.print(" " + nums[i]);
		}
		return count;
	}
}
